package com.ruijixiang.leetcode.weekly.leetcode2024.leetcode20240818;

public class leetcode3258 {
    /**
     * 设以i为右端点的合法子串，其左端点最小是left
     * @param s
     * @param k
     * @return
     */
    public int countKConstraintSubstring(String s,int k){
        char[] chs=s.toCharArray();
        int ans=0;
        int left=0;
        int[] cnt=new int[2];
        for(int i=0;i<chs.length;i++){
            cnt[chs[i] & 1]++;
            while(cnt[0]>k && cnt[1]>k){
                cnt[chs[left++] & 1]--;
            }
            ans+=i-left+1;
        }
        return ans;
    }
}
